jQuery(function(){
	jQuery("li.listTaskDoing").each(function(){
		var $this = jQuery(this);
		var taskId = $this.attr("rel");
		var previousClass = "";
		var previousRemoveClass = "";
//		$this.hover(function(){
//			if($this.hasClass("taskComingUp")){
//				previousClass = "taskComingUp";
//				$this.removeClass(previousClass);
//			}
//			if(!$this.hasClass("taskDone")){							
//				$this.addClass('taskDone');
//				appendTaskDoneIcon($this);
//			}					
//		}, function(){
//			if(!$this.hasClass("added")){
//				$this.removeClass('taskDone');
//				removeTaskDoneIcon($this);
//			}
//			if(previousClass != ""){
//				$this.addClass(previousClass);
//				previousClass = "";
//			}						
//		});
		$this.click(function(){
			if($this.hasClass("taskDone added")){
				$this.removeClass('taskDone added');
				$this.addClass(previousRemoveClass);
				previousClass = "";
				$this.find("a.iconTaskDone").remove();
				$this.children("input.finished").val(false);
			}else{
				if($this.hasClass("taskComingUp")){
					previousRemoveClass = "taskComingUp";
					$this.removeClass(previousRemoveClass);
				}
				$this.addClass('taskDone added');
				appendTaskDoneIcon($this);
				$this.children("input.finished").val(true);
			}	
			calculateCompletePercent();
		});
	});
	
	function appendTaskDoneIcon($selector){
		jQuery("<a class='iconTaskDone fl' href='#'></a>").appendTo($selector);
	}
	
	function removeTaskDoneIcon($selector){
		$selector.find("a.iconTaskDone").remove();
	}
	
	function calculateCompletePercent(){
		var $completedPercent = jQuery("#completedPercent");
		var val = 0;
		
		jQuery("li.taskDone").each(function(){
			var $taskDone = jQuery(this);
			var $percent = $taskDone.find(".taskPercent");
			var percent = $percent.text();
			percent = percent.substring(0, percent.length-1);
			percent = parseInt(percent);
			val += percent;
		});
		
		$completedPercent.text(val+"%");
	}
});

function submitForm(){
	jQuery("form[name=updateTasks]").submit();
}